<script setup lang="ts">
import {reqAttr} from "@/api/product/attr";
import {reqAddSku, reqSpuHasSaleAttr, reqSpuImageList} from "@/api/product/spu";
import {reactive, ref} from "vue";
import {SkuData} from "@/api/product/spu/type.ts";
import {ElMessage} from "element-plus";

const emit = defineEmits(['changeScene'])
const table = ref()

// 平台属性
const attrArr = ref<any>([])
// 销售属性
const saleArr = ref<any>([])
// 照片数据
const imgArr = ref<any>([])
// 手机SKU的参数
const skuParams = reactive<SkuData>({
  category3Id:"",
  spuId:"",
  tmId:"",
  skuName:"",
  price:"",
  weight:"",
  skuDesc:"",
  skuAttrValueList:[{
    attrId:"",
    valueId:""
  }],
  skuSaleAttrValueList:[{
    saleAttrId:"",
    saleAttrValueId:""
  }],
  skuDefaultImg:""
})
const cancel = () => {
  emit('changeScene', {
    flag:0,
    params:''
  })
}

const initSkuData = async (C1Id:number|string, C2Id:number|string, spu:any) => {
  skuParams.category3Id = spu.category3Id
  skuParams.spuId = spu.id
  skuParams.tmId = spu.tmId
  let result:any = await reqAttr(C1Id, C2Id, spu.category3Id)
  let result1:any = await reqSpuHasSaleAttr(spu.id)
  let result2:any = await reqSpuImageList(spu.id)
  attrArr.value = result.data
  console.log(result.data)
  saleArr.value = result1.data
  imgArr.value = result2.data
}

const defaultSetting = (row:any) => {
  imgArr.value.forEach((item:any)=> {
    table.value.toggleRowSelection(item, false)
  })
  table.value.toggleRowSelection(row, true)
  skuParams.skuDefaultImg = row.imgUrl
}

const save = async () => {
  skuParams.skuAttrValueList = attrArr.value.reduce((prev: any, next: any) => {
    if (next.attrIdAndValueId) {
      let [attrId, valueId] = next.attrIdAndValueId.split(':')
      prev.push({
        attrId,
        valueId
      })
    }
    return prev
  }, [])

  skuParams.skuSaleAttrValueList = saleArr.value.reduce((prev:any, next:any)=>{
    if(next.saleIdAndValueId) {
      let [saleAttrId, saleAttrValueId] = next.saleIdAndValueId.split(':')
      prev.push({
        saleAttrId,
        saleAttrValueId
      })
    }
    return prev
  }, [])

  const result = await reqAddSku(skuParams)
  if(result.code === 200) {
    ElMessage({
      type:'success',
      message:'添加成功'
    })
    emit('changeScene', {
      flag:0,
      params:''
    })
  }else {
    ElMessage({
      type:'error',
      message:'添加失败'
    })
  }
}

defineExpose({
  initSkuData
})
</script>

<template>
  <el-form>
    <el-form-item label="sku名称">
      <el-input placeholder="SKU名称" v-model="skuParams.skuName"></el-input>
    </el-form-item>
    <el-form-item label="价格(元)">
      <el-input placeholder="价格(元)" type="number" v-model="skuParams.price"></el-input>
    </el-form-item>
    <el-form-item label="重量(g)">
      <el-input placeholder="重量(g)" type="number" v-model="skuParams.weight"></el-input>
    </el-form-item>
    <el-form-item label="SKU描述">
      <el-input type="textarea" placeholder="SKU描述" v-model="skuParams.skuDesc"></el-input>
    </el-form-item>
    <el-form-item label="平台属性">
      <el-form :inline="true">
        <el-form-item style="margin-bottom: 10px" v-for="item in attrArr" :key="item.id" :label="item.attrName">
          <el-select v-model="item.attrIdAndValueId">
            <el-option :value="`${item.id}:${attrValue.id}`" v-for="attrValue in item.attrValueList" :key="attrValue.id" :label="attrValue.valueName"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="销售属性">
      <el-form :inline="true">
        <el-form-item style="margin-bottom: 10px" v-for="item in saleArr" :key="item.id" :label="item.saleAttrName">
          <el-select v-model="item.saleIdAndValueId">
            <el-option :value="`${item.id}:${saleAttrValue.id}`" v-for="saleAttrValue in item.spuSaleAttrValueList" :key="saleAttrValue.id" :label="saleAttrValue.saleAttrValueName"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="图片名称">
      <el-table ref="table" border :data="imgArr">
        <el-table-column type="selection" width="80px" align="center"></el-table-column>
        <el-table-column label="图片" align="center">
          <template v-slot="{row}">
            <img :src="row.imgUrl" alt="" style="width: 100px;height: 100%">
          </template>
        </el-table-column>
        <el-table-column prop="imgName" label="名称" align="center"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="{row}">
            <el-button @click="defaultSetting(row)" type="primary" size="large">
              设置默认
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form-item>
    <el-form-item>
      <el-button @click="save" type="primary" size="default">保存</el-button>
      <el-button @click="cancel" type="primary" size="default">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<style scoped lang="scss">

</style>